package com.wispx.gateway.core.container;

import com.wispx.gateway.config.config.Config;
import com.wispx.gateway.core.netty.NettyHttpClient;
import com.wispx.gateway.core.netty.NettyHttpServer;
import com.wispx.gateway.core.netty.processor.NettyCoreProcessor;

import java.util.concurrent.atomic.AtomicBoolean;

/**
 * 容器，将Client、Server组件组装
 */
public class Container implements Lifecycle {

    private NettyHttpClient nettyHttpClient;
    private NettyHttpServer nettyHttpServer;
    private AtomicBoolean init = new AtomicBoolean(false);

    public Container(Config config) {
        this.nettyHttpClient = new NettyHttpClient(config);
        this.nettyHttpServer = new NettyHttpServer(config, new NettyCoreProcessor());
    }

    @Override
    public void start() {
        if (!init.compareAndSet(false, true)) {
            return;
        }
        nettyHttpClient.start();
        nettyHttpServer.start();
    }

    @Override
    public void shutdown() {
        if (!init.get()) {
            return;
        }
        nettyHttpClient.shutdown();
        nettyHttpServer.shutdown();
    }

    @Override
    public boolean isStart() {
        return init.get();
    }
}
